package com.csair.seam.domain.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.csair.seam.domain.mapper.VehicleSituationMapper;
import com.csair.seam.domain.model.entity.VehicleSituation;
import com.csair.seam.domain.model.vo.VehicleSituationVo;
import com.csair.seam.domain.service.IVehicleSituationService;
@Service
public class VehicleSituationServiceImp implements IVehicleSituationService {
	
	@Autowired
	private VehicleSituationMapper vehicleSituationMapper;

	@DS("nc63")
	@Override
	public List<VehicleSituationVo> findVehicleSituation() {
		Map<String, VehicleSituationVo> map = new HashMap<>();
		List<VehicleSituation> generalVehicleSituation = vehicleSituationMapper.findVehicleSituation("11");//通用车辆总体情况
		for (VehicleSituation vehicleSituation : generalVehicleSituation) {
			VehicleSituationVo vo = new VehicleSituationVo();
			vo.setOrgName(vehicleSituation.getOrgName());
			vo.setGeneralVehicleTotalNum(vehicleSituation.getTotalNum());
			vo.setGeneralVehicleToalPrice(vehicleSituation.getToalPrice());
			vo.setGeneralVehicleAvgServiceYear(vehicleSituation.getAvgServiceYear());
			map.put(vehicleSituation.getOrgCode(), vo);
		}
		
		List<VehicleSituation> specialVehicleSituation = vehicleSituationMapper.findVehicleSituation("12");//特种车辆总体情况
		for (VehicleSituation vehicleSituation : specialVehicleSituation) {
			if(map.containsKey(vehicleSituation.getOrgCode())) {
				VehicleSituationVo vo = map.get(vehicleSituation.getOrgCode());
				vo.setSpecialVehicleTotalNum(vehicleSituation.getTotalNum());
				vo.setSpecialVehicleToalPrice(vehicleSituation.getToalPrice());
				vo.setSpecialVehicleAvgServiceYear(vehicleSituation.getAvgServiceYear());
			}else {
				VehicleSituationVo vo = new VehicleSituationVo();
				vo.setOrgName(vehicleSituation.getOrgName());
				vo.setSpecialVehicleTotalNum(vehicleSituation.getTotalNum());
				vo.setSpecialVehicleToalPrice(vehicleSituation.getToalPrice());
				vo.setSpecialVehicleAvgServiceYear(vehicleSituation.getAvgServiceYear());
				map.put(vehicleSituation.getOrgCode(), vo);
			}
		}
		
		//按通用车辆数量+特种车辆数量降序排序
		if(!map.isEmpty()) {
			List<VehicleSituationVo> list = map.values().stream().collect(Collectors.toList());
			Collections.sort(list, (e1,e2)->calTotalNum(e2).compareTo(calTotalNum(e1)));
			return list;
		}
		return Collections.emptyList();
	}
	
	private Integer calTotalNum(VehicleSituationVo vo) {
		return vo.getGeneralVehicleTotalNum()+vo.getSpecialVehicleTotalNum();
	}
	
}
